﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Ionic.Zip;

namespace ShadowEraDeckEditor
{
    public partial class FormDeckHack : Form
    {
        private Deckdb[] card = new Deckdb[2000];
        int[] player1cards = new int[250];
        int[] player2cards = new int[250];
        int player1cardsLength = 0;
        int player2cardsLength = 0;
        int player1cardsLeft = 0;
        int player2cardsLeft = 0;



        public FormDeckHack()
        {
            InitializeComponent();
            InitializeDeck();
        }

        private void InitializeDeck()
        {
            //read in entire file to "dblines"
            //const string f = "carddb.ini";
            List<string> dblines = new List<string>();
            ZipFile zip = ZipFile.Read("carddb.dat");
            zip.Password = "c@n7cr4kD1zSh!t";
            ZipEntry z = zip["carddb.ini"];
            MemoryStream carddatabase = new MemoryStream();
            z.Extract(carddatabase);
            carddatabase.Position = 0;
            using (StreamReader r = new StreamReader(carddatabase))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    dblines.Add(line);
                }
            }
            //run through entire file, lines where not {} create new card
            int dbcount = 0;
            foreach (string s in dblines)
            {
                bool test = s.StartsWith("[");
                if (!test)
                {
                    this.card[dbcount] = new Deckdb(s);
                    dbcount += 1;
                }
            }
            //write last card in arrary's ID to be equal to number of total cards in DB
            dbcount -= 1;
            string totalcount = dbcount.ToString() + ",a,a,a,a,a";
            card[1999] = new Deckdb(totalcount);
        }

        private void FormDeckHack_Load(object sender, EventArgs e)
        {
            this.Location = Owner.Location;
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox4.SizeMode = PictureBoxSizeMode.Zoom;
        }

        private void buttonHack_Click(object sender, EventArgs e)
        {
            string packet = "";
            if (textBoxPacket.Text != "")
                packet = Crypto.SEDecrypt(textBoxPacket.Text);
            string[] splitS = packet.Split('\n');
            textBoxPlayer1.Text = splitS[2];
            textBoxPlayer2.Text = splitS[4];

            //player 1 cards 
            string[] splitcards = splitS[3].Split(',');
            int temp = 0;
            for (int x = 0; x < splitcards.Length; x++)
            {
                int i;
                int.TryParse(splitcards[x], out i);
                if (i > 20)
                    if(temp == 0)
                    player1cards[x] = i;
                if(i <= 20)
                    temp = 1;
                if (i > 20)
                    if (temp == 1)
                        player1cards[x - 1] = i;
            }
            player1cardsLength = splitcards.Length - 1;
            //player 2 cards
            temp = 0;
            splitcards = splitS[5].Split(',');
            for (int x = 0; x < splitcards.Length; x++)
            {
                int i;
                int.TryParse(splitcards[x], out i);
                if (i > 20)
                    if (temp == 0)
                        player2cards[x] = i;
                if(i <= 20)
                    temp = 1;
                if (i > 20)
                    if (temp == 1)
                        player2cards[x - 1] = i;
            }
            player2cardsLength = splitcards.Length - 1;
            //set current card
            player1cardsLeft = player1cardsLength - 6;
            player2cardsLeft = player2cardsLength - 6;
            CardImages();
            textBoxP1count.Text = player1cardsLeft.ToString();
            textBoxP2count.Text = player2cardsLeft.ToString();
        }

        private void CardImages()
        {
            for (int x = 0; x <= card[1999].cardID; x++)
            {
                if (player1cards[player1cardsLeft-1] == card[x].cardID)
                {
                    pictureBox1.Image = Image.FromStream(card[x].cardURL);
                }
            }
            for (int x = 0; x <= card[1999].cardID; x++)
            {
                if (player1cards[player1cardsLeft - 2] == card[x].cardID)
                {
                    pictureBox2.Image = Image.FromStream(card[x].cardURL);
                }
            }
            for (int x = 0; x <= card[1999].cardID; x++)
            {
                if (player2cards[player2cardsLeft - 1] == card[x].cardID)
                {
                    pictureBox3.Image = Image.FromStream(card[x].cardURL);
                }
            }
            for (int x = 0; x <= card[1999].cardID; x++)
            {
                if (player2cards[player2cardsLeft - 2] == card[x].cardID)
                {
                    pictureBox4.Image = Image.FromStream(card[x].cardURL);
                }
            }
        }

        private void buttonDrawP1_Click(object sender, EventArgs e)
        {
            int i;
            int.TryParse(textBoxP1count.Text, out i);
            player1cardsLeft = i - 1;
            CardImages();
            textBoxP1count.Text = player1cardsLeft.ToString();
        }

        private void buttonDrawP2_Click(object sender, EventArgs e)
        {
            int i;
            int.TryParse(textBoxP2count.Text, out i);
            player2cardsLeft = i - 1;
            CardImages();
            textBoxP2count.Text = player2cardsLeft.ToString();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((FormMenu)Owner).nextform = 5;
            this.Close();
        }

        private void deckBuilderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Owner.Location = this.Location;
            ((FormMenu)Owner).nextform = 1;
            this.Close();
        }

        private void deckHackLiteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Owner.Location = this.Location;
            ((FormMenu)Owner).nextform = 3;
            this.Close();
        }
    }
}
